<template>
  <div>
    <template v-if="showDetail">
      <el-card>
        <div>
          <h2>请假详情</h2>
          <el-descriptions :column="1" border>
            <el-descriptions-item label="ID">{{ currentLeave.id }}</el-descriptions-item>
            <el-descriptions-item label="开始时间">{{ currentLeave.startDate }}</el-descriptions-item>
            <el-descriptions-item label="结束时间">{{ currentLeave.endDate }}</el-descriptions-item>
            <el-descriptions-item label="请假原因">{{ currentLeave.reason }}</el-descriptions-item>
            <el-descriptions-item label="状态">{{ statusText(currentLeave.status) }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{ currentLeave.createTime }}</el-descriptions-item>
          </el-descriptions>
          <el-button class="mt-4" @click="handleBack">返回</el-button>
        </div>
      </el-card>
    </template>
    <template v-else>
      <el-card>
        <div class="mb-4">
          <el-button type="primary" @click="handleAdd">新增请假</el-button>
        </div>
        
        <el-table :data="leaveList" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="startDate" label="开始时间" />
          <el-table-column prop="endDate" label="结束时间" />
          <el-table-column prop="reason" label="请假原因" />
          <el-table-column v-if="false" prop="status" label="状态">
            <template #default="{ row }">
              <el-tag :type="statusType(row.status)">
                {{ statusText(row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column label="操作" width="220">
            <template #default="{ row }">
              <el-button size="small" @click="handleDetail(row)">详情</el-button>
              <el-button size="small" @click="handleEdit(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </template>
    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker 
            v-model="form.startDate" 
            type="date" 
            placeholder="选择开始日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker 
            v-model="form.endDate" 
            type="date" 
            placeholder="选择结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="请假原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status" v-if="!isAdd">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="待审批" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="已拒绝" :value="2" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  listLeaveapplication, 
  addLeaveapplication, 
  updateLeaveapplication, 
  delLeaveapplication 
} from '@/api/leaveapplication'

const leaveList = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isAdd = ref(true)
const formRef = ref(null)

const form = reactive({
  id: null,
  startDate: '',
  endDate: '',
  reason: '',
  status: 0
})

const rules = {
  startDate: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
  endDate: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
  reason: [{ required: true, message: '请输入请假原因', trigger: 'blur' }]
}

// 状态文本转换
const statusText = (status) => {
  const map = {
    0: '待审批',
    1: '已通过',
    2: '已拒绝'
  }
  return map[status] || '未知'
}

// 状态标签类型
const statusType = (status) => {
  const map = {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }
  return map[status] || ''
}

// 获取请假记录列表
const getLeaveList = async () => {
  try {
    const response = await listLeaveapplication()
    if (response) {
      leaveList.value = response
    } else {
      ElMessage.error('获取请假列表失败: 数据格式异常')
    }
  } catch (error) {
    ElMessage.error(`获取请假列表失败: ${error.message}`)
  }
}

// 新增请假记录
const handleAdd = () => {
  dialogTitle.value = '新增请假'
  isAdd.value = true
  Object.assign(form, {
    id: null,
    startDate: '',
    endDate: '',
    reason: '',
    status: 0
  })
  dialogVisible.value = true
}

// 编辑请假记录
const handleEdit = (row) => {
  dialogTitle.value = '编辑请假'
  isAdd.value = false
  Object.assign(form, row)
  dialogVisible.value = true
}

// 删除请假记录
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该请假记录?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await delLeaveapplication(row.id)
    ElMessage.success('删除成功')
    getLeaveList()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 提交表单
const submitForm = async () => {
  await formRef.value.validate()
  try {
    if (isAdd.value) {
      await addLeaveapplication(form)
      ElMessage.success('新增成功')
    } else {
      await updateLeaveapplication(form)
      ElMessage.success('更新成功')
    }
    dialogVisible.value = false
    getLeaveList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

onMounted(() => {
  getLeaveList()
})

const showDetail = ref(false)
const currentLeave = ref({})

const handleDetail = (row) => {
  currentLeave.value = row
  showDetail.value = true
}

const handleBack = () => {
  showDetail.value = false
}
</script>